package cqrtplm.aop;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.classification.model.vo.TyplmClassificationVO;
import com.hustcad.plm.pdm.classification.service.TyplmClassificationCacheService;
import com.hustcad.plm.pdm.iba.mapper.TyplmAttributeDefMapper;
import com.hustcad.plm.pdm.iba.mapper.TyplmAttributeDefaultValueMapper;
import com.hustcad.plm.pdm.iba.model.dto.AttributeDefDTO;
import com.ty.basic.entity.attribute.AttributeDefMapDO;
import com.ty.basic.entity.attribute.AttributeDefaultValueDO;
import com.ty.basic.vo.AttributeVO;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.mapper.RTAttributeCardMapper;
import cqrtplm.mapper.RTPartMapper;
import cqrtplm.mapper.RTTypeClassMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Aspect
@Component
@Slf4j
public class RTAttributeAop {
    @Resource
    private RTAttributeCardMapper rtAttributeCardMapper;
    @Resource
    private cqrtplm.mapper.ProcessNumberMapper processNumberMapper;
    @Resource
    private RTTypeClassMapper rtTypeClassMapper;
    @Resource
    private TyplmAttributeDefaultValueMapper attributeDefaultValueMapper;

    @Resource
    private TyplmClassificationCacheService typlmClassificationCacheService;

    /**
     * 创建属性定义
     */
    @Around(value = "execution(* com.hustcad.plm.pdm.iba.service.TyplmAttributeDefService.createAttrDef(..))")
    public Object createAttributeAop(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        AttributeDefDTO attributeDefDTO = (AttributeDefDTO) args[0];
        String displayName = attributeDefDTO.getDisplayname();
        List<AttributeVO> dbDefList = rtAttributeCardMapper.queryAttrDefByDisName(
                Collections.singletonList(displayName));
        if (!CollectionUtils.isEmpty(dbDefList)) {
            //            TyException.throwSingleTyExceptionVarargs(ResponseCodeEnum.ACC_PARAMETER_OBJECT_EXISTS, displayName);
        }
        if (StringUtils.isEmpty(attributeDefDTO.getName()) || StringUtils.isEmpty(attributeDefDTO.getName())) {
            String attributeGroupName = rtAttributeCardMapper.selectAttrGroupNameById(
                    String.valueOf(attributeDefDTO.getAttributegroupoid()));
            //            BigInteger attributeGroupOid = attributeDefDTO.getAttributegroupoid();
            //            String attributeGroupEnName = rtAttributeCardMapper.selectAttributeGroupNameById(attributeGroupOid);
            String ibaAttrCode = processNumberMapper.generateIbaAttrCode();
            if (StringUtils.isNotEmpty(attributeGroupName) && attributeGroupName.contains("枚举")) {
                attributeDefDTO.setName("enum@@@" + ibaAttrCode);
            } else if (StringUtils.isNotEmpty(attributeGroupName) && attributeGroupName.contains("颜色")) {
                attributeDefDTO.setName("color@@@" + ibaAttrCode);
            } else {
                attributeDefDTO.setName(ibaAttrCode);
            }

        }
        return point.proceed();
    }

    @Resource
    private TyplmAttributeDefMapper typlmAttributeDefMapper;

    /**
     * 删除绑定的属性时检查是否被零部件使用
     *
     * @param point
     * @return
     * @throws Throwable
     */
    @Around(value = "execution(* com.hustcad.plm.pdm.type.service.TyplmTypeService.deleteTypeIba(..))")
    public Object deleteAttributeAop(ProceedingJoinPoint point) throws Throwable {
        Object[] args = point.getArgs();
        BigInteger attributeId = (BigInteger) args[0];
        AttributeDefMapDO attributeDefMapDO = typlmAttributeDefMapper.selectByPrimaryKey(attributeId);
        BigInteger attributereferenceOid = attributeDefMapDO.getTypeoid();
        List<TyplmClassificationVO> resultList = typlmClassificationCacheService.queryClassificationByParentID(
                attributereferenceOid, "N");
        if (CollUtil.isNotEmpty(resultList)) {
            List<String> classOidList = new ArrayList<>();
            for (TyplmClassificationVO typlmClassificationVO : resultList) {
                classOidList.add(typlmClassificationVO.getId().toString());
            }
            List<JSONObject> objectList = rtTypeClassMapper.selectPartNumberByAttributereferenceoidAndClassOid(
                    String.valueOf(attributeDefMapDO.getAttributereferenceoid()),
                    classOidList);
            if (CollUtil.isNotEmpty(objectList)) {
                throw RTErrorCodeEnum.ENUM_VALUE_NOT_EXIST.getException("存在实例化数据，不允许删除！");
            }
        } else {
            List<JSONObject> objectList = rtTypeClassMapper.selectPartNumberByAttributereferenceoidAndClassOid(
                    String.valueOf(attributeDefMapDO.getAttributereferenceoid()),
                    Collections.singletonList(attributereferenceOid.toString()));
            if (CollUtil.isNotEmpty(objectList)) {
                throw RTErrorCodeEnum.ENUM_VALUE_NOT_EXIST.getException("存在实例化数据，不允许删除！");
            }
        }
        return point.proceed();
    }
}
